﻿using ChestPainCenterSystem.Areas.ChestPainCenter.Models;
using ChestPainCenterSystem.Bll;
using ChestPainCenterSystem.Code.Bll;
using ChestPainCenterSystem.Core;
using ChestPainCenterSystem.Model;
using ChestPainCenterSystem.Utils;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace ChestPainCenterSystem.Areas.ChestPainCenter.Controllers
{
    public class PatientListController : BaseController
    {
        //
        // GET: /ChestPainCenter/PatientList/

        /// <summary>
        /// 页面加载
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 患者总览加载
        /// </summary>
        /// <returns></returns>
        public ContentResult Search()
        {
            //name=&status=0&pathway=0&cometype=0&page=1&pagesize=30
            string arrivedType = WebUtils.GetString("cometype");
            if (arrivedType.Equals("0")) arrivedType = string.Empty;
            string name = WebUtils.GetString("name");
            int status = WebUtils.GetInt("status");
            int pathwayId = WebUtils.GetInt("pathway");
            int pageIndex = WebUtils.GetInt("page", 1);
            int pageSize = WebUtils.GetInt("pageSize", 50);
            OperateResult resultInfo = PatientManager.GetPatientOverview(pageIndex, pageSize, name, status, pathwayId, arrivedType);
            string NoData = "{\"Total\": 0,\"Rows\": [], \"InHospitalNum\": 0, \"PreHospitalNum\": 0, \"PathWayNum\": 0}";
            if (resultInfo.Result == 1)
            {
                var listData = JsonConvert.DeserializeObject<dynamic>(resultInfo.Body.ToString());
                var result = new { Total = listData.PatientNum.Value, Rows = listData.PatientList, InHospitalNum = listData.InHospitalNum.Value, PreHospitalNum = listData.PreHospitalNum.Value, PathWayNum = listData.PathWayNum.Value };
                return JsonDate(result);
            }
            else
            {
                NoData = "{\"Total\": 0,\"Rows\":[],\"Result\":\"" + resultInfo.Result + "\",\"Body\":\"" + resultInfo.Body + "\",\"InHospitalNum\": 0, \"PreHospitalNum\": 0, \"PathWayNum\": 0}";
            }
            return Content(NoData);
        }

        /// <summary>
        /// 添加/编辑
        /// </summary>
        /// <returns></returns>
        public ActionResult Edit()
        {
            //获取传输过来的患者ID
            string patientId = WebUtils.GetString("id");
            PatientOverview overview = new PatientOverview();
            if (!string.IsNullOrWhiteSpace(patientId) && !patientId.Equals("0"))
            {
                //取值 赋值
                OperateResult resultInfo = PatientManager.GetPatientBaseInfor(patientId.ToString());
                string json = resultInfo.Body.ToString();
                PatientOverview overviewData = JsonConvert.DeserializeObject<PatientOverview>(json);
                overview.ConditionAssessment = overviewData.ConditionAssessment;
                overview.ID = overviewData.ID;
                overview.EMS_DISEASE_ADDR = overviewData.EMS_DISEASE_ADDR;
                overview.EMS_DISEASE_TIME = overviewData.EMS_DISEASE_TIME;
                overview.HelpTime = overviewData.HelpTime;
                overview.IsHelp = overviewData.IsHelp;
                overview.PATIENT_ADDR = overviewData.PATIENT_ADDR;
                overview.PATIENT_AGE = overviewData.PATIENT_AGE;
                overview.PATIENT_ARRIVE_CLASS = overviewData.PATIENT_ARRIVE_CLASS;
                overview.PATIENT_ID = overviewData.PATIENT_ID;
                overview.PATIENT_ID_NUM = overviewData.PATIENT_ID_NUM;
                overview.PATIENT_NAME = overviewData.PATIENT_NAME;
                overview.PATIENT_PHONE = overviewData.PATIENT_PHONE;
                overview.PATIENT_SEX = overviewData.PATIENT_SEX;
                overview.Hospital_ID = overviewData.Hospital_ID;
                overview.RegisterTime = overviewData.RegisterTime;
            }
            return View(overview);
        }
        /// <summary>
        /// 根据ID保存一个患者 新增/编辑
        /// </summary>
        /// <returns></returns>
        public ContentResult Save()
        {
            PatientOverview overview = new PatientOverview();
            overview.ID = WebUtils.GetInt("txtId");
            //性别
            overview.PATIENT_SEX = WebUtils.GetString("eventGrade") == "0" ? "男" : WebUtils.GetString("eventGrade") == "1" ? "女" : "";
            //病情评估
            overview.ConditionAssessment = WebUtils.GetString("illestimateListArray").Trim();
            //发病地址
            overview.EMS_DISEASE_ADDR = WebUtils.GetString("txtillAddress").Trim();
            //发病时间
            string ems_disease_time = WebUtils.GetString("txtillTime").Trim();
            if (!string.IsNullOrEmpty(ems_disease_time))
            {
                ems_disease_time += ":00";
                overview.EMS_DISEASE_TIME = Convert.ToDateTime(ems_disease_time);
            }
            //是否呼救
            overview.IsHelp = WebUtils.GetInt("rdoHelpList");
            //年龄
            overview.PATIENT_AGE = WebUtils.GetString("txtAge").Trim();
            //来院方式
            overview.PATIENT_ARRIVE_CLASS = WebUtils.GetString("txtfromhospitalType").Trim();
            //住院ID
            overview.PATIENT_ID = WebUtils.GetString("txtPATIENT_ID").Trim();
            //门诊ID
            overview.Hospital_ID = WebUtils.GetString("txtHospitalId").Trim();
            //患者姓名
            overview.PATIENT_NAME = WebUtils.GetString("txtRealName").Trim();
            //联系电话
            overview.PATIENT_PHONE = WebUtils.GetString("txtPhoneNum").Trim();
            //住址
            overview.PATIENT_ADDR = WebUtils.GetString("txtAddress").Trim();
            //身份证
            overview.PATIENT_ID_NUM = WebUtils.GetString("txtIDCard").Trim();
            //呼救时间
            string helptime = WebUtils.GetString("txthelpTime").Trim();
            if (!string.IsNullOrEmpty(helptime))
            {
                helptime += ":00";
                overview.HelpTime = Convert.ToDateTime(helptime);
            }
            //登记时间
            string registerTime = WebUtils.GetString("txtCreateTime").Trim();
            if (!string.IsNullOrEmpty(registerTime))
            {
                registerTime += ":00";
                overview.RegisterTime = Convert.ToDateTime(registerTime);
            }
            string oiverviewJson = JsonConvert.SerializeObject(overview).Trim();
            OperateResult resultInfo = PatientManager.SetPatientOverview(oiverviewJson);
            return JsonDate(resultInfo);
        }


        /// <summary>
        /// 获取诊疗路径名称
        /// ID Name
        /// </summary>
        /// <returns></returns>
        public ActionResult GetPathNameList()
        {
            OperateResult resultInfo = PathwayManager.GetPathNameList();
            if (resultInfo.Result == 1 && resultInfo.Body.ToString().IndexOf("[{") == 0 && resultInfo.Body.ToString().IndexOf("}]") > 0 && resultInfo.Body.ToString().IndexOf(":") > 0)
                return Content(resultInfo.Body.ToString().Replace("[", "[{\"ID\":\"0\",\"Name\":\"全部\"},"));
            return Content("[{\"ID\":0,\"Name\":\"全部\"}]");
        }

        /// <summary>
        /// 作废患者
        /// </summary>
        /// <returns></returns>
        public ActionResult GiveUp()
        {
            string patientId = WebUtils.GetString("patientId");
            OperateResult resultInfo = new OperateResult();
            resultInfo = PatientManager.DeletePatientRecord(patientId);
            return JsonDate(resultInfo);
        }

        public ActionResult WristPrint()
        {
            string pid = WebUtils.GetString("pid");
            if (string.IsNullOrWhiteSpace(pid))
                return Content("缺少ID参数");
            ViewBag.pid = pid;
            return View();
        }
    }
}
